package com.example.myspringbootproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myspringbootproject.mapper.CollegeMapper;
import com.example.myspringbootproject.mapper.SpecialityMapper;
import com.example.myspringbootproject.pojo.College;
import com.example.myspringbootproject.pojo.Speciality;
import com.example.myspringbootproject.service.impl.CollegeServiceImpl;
import com.example.myspringbootproject.service.impl.SpecialityServiceImpl;
import com.example.myspringbootproject.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/exam/college")
public class collegeController {

    @Autowired
    @Lazy
    CollegeServiceImpl collegeService;
    private College three;
    @Autowired
    CollegeMapper collegeMapper;
    /**
     * 获取用户列表
     * 查询以及分页
     */
    @GetMapping(value = "/list")

    public Result list(int currentPage, int pageSize, String content) {
        System.out.println("当前页："+currentPage+"查询条数："+pageSize+"查询条件"+content);
        //条件查询
        QueryWrapper<College> qw = new QueryWrapper<>();
        //判断查询
        qw.like("name",content).or().like("content",content);;



        Page<College> page = new Page<>(currentPage,pageSize);
        IPage<College> page1 = collegeService.page(page,qw);


        Map<String, Object> map = new HashMap<>();
        map.put("list", page1.getRecords());
        map.put("total", page1.getTotal());

        return Result.ok(map);

    }
    //新增
    @PostMapping(value = "/addUser")
    public Result addUser(@RequestBody College college){
        System.out.println("添加的数据是："+ college);

        //执行添加操作
        boolean save = collegeService.save(college);
        return save?Result.ok():Result.fail();
    }

    //删除
    @GetMapping (value = "/delete")
    public Result delete(@RequestParam List<Long> ids) {
        System.out.println("ids==="+ids);

        boolean b=collegeService.removeBatchByIds(ids);
        return b ? Result.ok() : Result.fail();

    }

    //修改
    @PostMapping(value = "/updateUser")
    public Result updateUser(@RequestBody College college){
        System.out.println("修改的数据是："+ college);
        //将添加对象的用户信息补充完整
        boolean update = collegeService.updateById(college);
        //执行添加操作

        return update ? Result.ok():Result.fail();
    }
}
